package project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import project.dao.OptionMapper;
import project.model.Option;
import project.utils.GetRandom;

import java.security.SecureRandom;
import java.util.List;
import java.util.Random;

@Service
public class OptionServiceImpl implements OptionService {
    @Autowired
    OptionMapper optionMapper;
    GetRandom getRandom = new GetRandom();
    @Override
    public int addOption(Option option) {
        return optionMapper.addOption(option);
    }

    @Override
    public List<Option> getOptionsByTid(int tid) {
        return optionMapper.getOptionsByTid(tid);
    }

    @Override
    public int updateOption(Option option) {
        return optionMapper.updateOption(option);
    }

    @Override
    public int deleteOption(int oid) {
        return optionMapper.deleteOption(oid);
    }

    @Override
    public Option generateRandom(List<Option> options) {
        /**
         * 从选项列表中随机选择一个选项
         *
         * @param options 候选选项列表（必须非空）
         * @return 随机选中的选项
         * @throws IllegalArgumentException 如果选项列表为空或为null
         */
        if (options == null || options.isEmpty()) {
            throw new IllegalArgumentException("选项列表不能为空");
        }

        // 2. 生成随机索引
//        Random random = new SecureRandom(); // 使用加密安全的随机数生成器
//        int randomIndex = random.nextInt(options.size());
        Option result = getRandom.generateHybridRandomResult(options);
        System.out.println("select fininshed successfully,result is:"+result.getContent());

        // 3. 返回随机选项
        return result;

    }
}
